package com.study.rabbitmq.advance.transaction;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class Send {
	private final static String QUEUE_NAME = "tanQueue";
	private final  static  String EXCHANGE_NAME = "tanExchange";

	private final  static  String ROUTING_KEY = "tan";

	public static void main(String[] args) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.20.68");
		factory.setPort(5672);
		factory.setUsername("leke");
		factory.setPassword("leke@@@");

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT,false,false,null);

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);


		channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY);


		String message = "Hello wrorld";



        try {
            //将当前Channel设置成是transaction模式
            channel.txSelect();

			for (int i=0; i<100; i++){
				channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, (message+i).getBytes());
//				if(i==10){
//					throw new Exception("xxxxx");
//				}
			}
            channel.txCommit();
        } catch (Exception e) {
            channel.txRollback();
        }

		channel.close();
		connection.close();
	}
}
